import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'app_info.dart';
import 'module_context.dart';

abstract class AppContext{
  static late AppContext _instance;
  late SharedPreferences _sharedPref;
  List<ModuleContext> modules = [];

  static AppContext get of {
    return _instance;
  }

  AppContext onNewInstance();


  Future<void> init({bool withoutFlutterBindingInitialized = false}) async {
    AppContext._instance = onNewInstance();

    if (!withoutFlutterBindingInitialized) {
      WidgetsFlutterBinding.ensureInitialized();
    }

    _sharedPref = await onInitSharedPreference();
    /// TODO 这里耗时比较高
    await AppInfo.init();
    // _accountManager = AccountManager.of;
    // _myVehicleProvider = MyVehicleProvider.of;
    // _memberInfoProvider = MemberInfoProvider.of;
    // _environmentProvider = EnvironmentProvider.of;
    // _configProvider = ConfigProvider.of;
    // _memCache = MemCache.defaultImpl;
    // _localeProvider = LocaleProvider.of;
    // _dialogPriorityManager = DialogPriorityManager.of;
    //
    // _environmentProvider.init(_sharedPref);
    // _localeProvider.init(_sharedPref);
    //
    // modules.addAll(onInstallModules());

    await onInit();

    // _apiClient = await onInitApiClient();

  }

  Future<void> onInit();

  Future<SharedPreferences> onInitSharedPreference();

  Future<Dio> onInitApiClient();
  installModule(ModuleContext module) {
    modules.add(module);
  }

  uninstallModule(ModuleContext module) {
    modules.remove(module);
  }
}